package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

import java.util.List;

/**
 * Created by jiangzx on 2017/3/31.
 */

public class SelectorPop extends PopupWindow {
    public static class PopData{
        public String code;
        public String name;
        public PopData(String code,String name){
            this.code=code;
            this.name=name;
        }
    }

    /**设置当前选中项*/
    public void setCurrentSelected(String code){
        if(!TextUtils.isEmpty(code)){
            adapter.setSelectedCode(code);
        }
    }

    public interface OnItemClicked{
        void onClicked(PopData itemData);
    }

    private SelectorAdapter adapter;

    public SelectorPop(Context context, final List<PopData> data,final OnItemClicked listener){
        setAnimationStyle(R.style.popwindow_anim_style);
        final View contentView= LayoutInflater.from(context).inflate(R.layout.pop_cancel_ensure_list,null);
        contentView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        contentView.findViewById(R.id.tv_ensure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if(listener!=null){
                    listener.onClicked(adapter.selected);
                }
            }
        });
        ListView listView= (ListView) contentView.findViewById(R.id.list_view);
        adapter=new SelectorAdapter(context,data);
        listView.setAdapter(adapter);
        setContentView(contentView);
        setFocusable(true);
        setTouchable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight((int) (ScreenUtil.getScreenHeight()*0.44f));
        setBackgroundDrawable(new ColorDrawable(0xffffffff));
    }

    private class SelectorAdapter extends BaseAdapter{
        private PopData selected;
        private Context context;
        private List<PopData> data;
        private SelectorAdapter(Context context,List<PopData> data){
            this.context=context;
            this.data=data;
        }
        @Override
        public int getCount() {
            return data==null?0:data.size();
        }

        @Override
        public Object getItem(int position) {
            return data==null?null:data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
            if(convertView==null){
                textView=new TextView(context);
                AbsListView.LayoutParams layoutParams=
                        new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                                DensityUtil.dp2px(45));
                textView.setLayoutParams(layoutParams);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
                textView.setGravity(Gravity.CENTER);
            }else{
                textView= (TextView) convertView;
            }
            final PopData d=data.get(position);
            textView.setText(d.name);
            if(d==selected){
                textView.setTextColor(0xffff9e27);
            }else{
                textView.setTextColor(0xffcccccc);
            }
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected(d);
                }
            });
            return textView;
        }

        private void setSelected(PopData selected){
            if(selected!=this.selected){
                this.selected=selected;
                notifyDataSetChanged();
            }
        }

        private void setSelectedCode(String selectedCode){
            PopData popData;
            for(int i=0,size=data.size();i<size;i++){
                popData=data.get(i);
                if(popData.code.equals(selectedCode)){
                    setSelected(popData);
                    break;
                }
            }
        }
    }
}
